//导入Ajax
import {
    ajax,
    isObject
} from "./ajax.js"
var getCookie = function getCookie(key) {
    var arr1 = document.cookie.split('; ')
    //["user1=111asdasd", "user2=222gfhft", "user3=333wwwaa"]
    for (var i = 0; i < arr1.length; i++) {
        var arr2 = arr1[i].split('=') //['user1','111asdasd']
        if (arr2[0] === key) {
            // 对数据进行解码并返回
            return unescape(arr2[1])
        }
    }
    return null
}
var cararr = true;
var carharr = [];
var userid = ''
//判断是否登录成功
if(getCookie("username")&&getCookie("loginsucess")=="true"){
    userid = getCookie("username")
}else{

}
//查看收藏夹是否有商品
$.post('./php/chat.php', {
    name: userid,
    way: 'seeCollect',
}, function (data) {
    if(data){
        data = JSON.parse(data)
    }
    
    var goods = []
    for (var i = 0; i < data.length; i++) {
        goods.push(data[i]['collect'])
    }
    if(goods){
        goods = JSON.stringify(goods)
    }
    
    
    $.post('./php/chat.php', {
        goods: goods,
        way: 'collectPage',
    }, function (data) {
        if(data){
        carharr = JSON.parse(data)
        }
        if (carharr[0]) {
            console.log(1)
            cararr = false;
        } else {
            cararr = true;
        }
        // 渲染页面
        if (cararr) {
            $(".content .havegoods")[0].style.display = "none"
            $(".content .empty")[0].style.display = "block"
        } else {
            $(".content .empty")[0].style.display = "none"
        }
        var str = ''
        for (var i = 0; i < carharr.length; i++) {
                    str += `
                    <div class="goods ">
                    <div>
                        <a href="./detail.html?bname=${escape(carharr[i]['id'])}">
                            <img src=${carharr[i]["url"]} alt="">
                        </a>
                        <a href="./detail.html?bname=${escape(carharr[i]['id'])}" class="bookname">
                            <span>
                            ${carharr[i]["goodsname"]}
                            </span>
                        </a>
                    </div>
                    <div>${carharr[i]["price2"]}</div>
                    <div>
                    ${carharr[i]["price2"].split("￥")[1]*10}
                    </div>
                    <div>
                    ${carharr[i]["shop"]}
                    </div>
                </div>
                    `
        }
        $(".carcont").html(str)
    });
});





//计算总价*******************